/*     */ package cc.s2m.web.s2mBlog.controller.base;
/*     */ 
/*     */ import cc.s2m.util.CookieUtil;
import cc.s2m.web.s2mBlog.util.MemcacheKeys;
import cc.s2m.web.s2mBlog.util.StaticProp;
import net.spy.memcached.MemcachedClient;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */ 
/*     */ public class BaseController extends ExceptionBaseController
/*     */ {
/*  24 */   protected final int sessionOutTime = 1800;
/*     */ 
/*     */   @Autowired
/*     */   private MemcachedClient memcachedClient;
/*     */ 
/*     */   public Object getMemObjectFromCookie(MemcacheKeys memcacheKeys, HttpServletRequest request)
/*     */   {
/*  32 */     CookieUtil cookie = new CookieUtil(request, null);
/*  33 */     String cookieId = cookie.getCookie(StaticProp.cookieID);
/*  34 */     if (Strings.isBlank(cookieId)) {
/*  35 */       return null;
/*     */     }
/*  37 */     Object t = this.memcachedClient.get(memcacheKeys.getKey() + cookieId);
/*  38 */     this.memcachedClient.replace(memcacheKeys.getKey() + cookieId, 1800, t);
/*  39 */     return t;
/*     */   }
/*     */ 
/*     */   public Object getMemObjectFromCookie(MemcacheKeys memcacheKeys, HttpServletRequest request, Class type)
/*     */   {
/*  45 */     return getMemObjectFromCookie(memcacheKeys, request);
/*     */   }
/*     */ 
/*     */   public void setMemObjectFromCookie(MemcacheKeys memcacheKeys, HttpServletRequest request, HttpServletResponse response, Object o)
/*     */   {
/*  52 */     CookieUtil cookie = new CookieUtil(request, response);
/*  53 */     String cookieId = cookie.getCookie(StaticProp.cookieID);
/*  54 */     if (Strings.isBlank(cookieId)) {
/*  55 */       return;
/*     */     }
/*  57 */     this.memcachedClient.replace(memcacheKeys.getKey() + cookieId, 1800, o);
/*     */   }
/*     */ 
/*     */   public void delMemObjectFromCookie(MemcacheKeys memcacheKeys, HttpServletRequest request, HttpServletResponse response)
/*     */   {
/*  64 */     CookieUtil cookie = new CookieUtil(request, null);
/*  65 */     String cookieId = cookie.getCookie(StaticProp.cookieID);
/*  66 */     if (Strings.isBlank(cookieId)) {
/*  67 */       return;
/*     */     }
/*  69 */     this.memcachedClient.delete(memcacheKeys.getKey() + cookieId);
/*     */   }
/*     */ 
/*     */   public boolean checkCodeIsEqual(HttpServletRequest request, String picCode)
/*     */   {
/*  82 */     if ((picCode == null) || (picCode.trim().isEmpty())) {
/*  83 */       return false;
/*     */     }
/*  85 */     CookieUtil cookie = new CookieUtil(request, null);
/*  86 */     String cookieId = cookie.getCookie(StaticProp.cookieID);
/*  87 */     if ((cookieId == null) || (cookieId.trim().isEmpty())) {
/*  88 */       return false;
/*     */     }
/*  90 */     String code_m = (String)this.memcachedClient.get(MemcacheKeys.PIC_CODE.getKey() + cookieId);
/*  91 */     if ((code_m == null) || (code_m.trim().isEmpty())) {
/*  92 */       return false;
/*     */     }
/*  94 */     if (!picCode.equalsIgnoreCase(code_m)) {
/*  95 */       long errorNum = this.memcachedClient.incr(MemcacheKeys.PIC_CODE.getKey() + cookieId + "_errorsNum", 1, 1L, 300);
/*  96 */       if (errorNum > 5L) {
/*  97 */         this.memcachedClient.delete(MemcacheKeys.PIC_CODE.getKey() + cookieId);
/*  98 */         this.memcachedClient.delete(MemcacheKeys.PIC_CODE.getKey() + cookieId + "_errorsNum");
/*     */       }
/* 100 */       return false;
/*     */     }
/* 102 */     this.memcachedClient.delete(MemcacheKeys.PIC_CODE.getKey() + cookieId);
/* 103 */     this.memcachedClient.delete(MemcacheKeys.PIC_CODE.getKey() + cookieId + "_errorsNum");
/* 104 */     return true;
/*     */   }
/*     */ 
/*     */   public Map<String, String> upload(MultipartFile upfile)
/*     */   {
/* 112 */     Map resultJson = new HashMap();
/* 113 */     if ((upfile == null) || (upfile.getSize() < 1L)) {
/* 114 */       resultJson.put("msg", "未包含文件上传域");
/* 115 */       return resultJson;
/*     */     }
/* 117 */     String fileName = upfile.getOriginalFilename();
/* 118 */     Iterator type = Arrays.asList(StaticProp.allowFiles).iterator();
/* 119 */     boolean allowFilesFlag = false;
/* 120 */     while (type.hasNext()) {
/* 121 */       String ext = (String)type.next();
/* 122 */       if (fileName.toLowerCase().endsWith(ext)) {
/* 123 */         allowFilesFlag = true;
/*     */       }
/*     */     }
/* 126 */     if (!allowFilesFlag) {
/* 127 */       resultJson.put("msg", "不允许的文件格式");
/* 128 */       return resultJson;
/*     */     }
/* 130 */     String url = StaticProp.upYunPath + DigestUtils.md5Hex(UUID.randomUUID().toString());
/* 131 */     url = url + fileName.substring(fileName.lastIndexOf("."));
/*     */     try {
/* 133 */       boolean a = StaticProp.upyun.writeFile(url, upfile.getBytes(), true);
/* 134 */       if (!a)
/* 135 */         resultJson.put("msg", "上传到upyun失败");
/*     */     }
/*     */     catch (Exception e) {
/* 138 */       e.printStackTrace();
/* 139 */       resultJson.put("msg", e.getMessage());
/* 140 */       return resultJson;
/*     */     }
/* 142 */     resultJson.put("msg", "SUCCESS");
/* 143 */     resultJson.put("size", String.valueOf(upfile.getSize()));
/* 144 */     resultJson.put("originalName", fileName);
/* 145 */     resultJson.put("name", url);
/* 146 */     resultJson.put("url", "http://img.s2m.cc/" + url);
/* 147 */     resultJson.put("type", fileName.substring(fileName.lastIndexOf(".")));
/* 148 */     return resultJson;
/*     */   }
/*     */ }

/* Location:           C:\Users\Administrator\Desktop\s2mBlog.zip
 * Qualified Name:     s2mBlog-3.0.0.WEB-INF.classes.cc.s2m.web.s2mBlog.controller.base.BaseController
 * JD-Core Version:    0.6.1
 */